<template>
  <div>
    <h2>课题管理</h2>
    <el-table :data="topics" style="width: 100%">
      <el-table-column prop="id" label="课题编号" width="120" />
      <el-table-column prop="name" label="课题名称" />
      <el-table-column prop="teacher" label="指导教师" />
      <el-table-column prop="status" label="状态" />
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button size="mini" @click="editTopic(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteTopic(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑课题弹窗 -->
    <el-dialog title="编辑课题" v-model="dialogVisible" width="400px">
      <el-form :model="editForm" label-width="80px">
        <el-form-item label="课题名称">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="指导教师">
          <el-input v-model="editForm.teacher" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="editForm.status">
            <el-option label="可选" value="可选" />
            <el-option label="已满" value="已满" />
            <el-option label="关闭" value="关闭" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveTopic">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const topics = ref([
  { id: 'K001', name: '智能导航系统', teacher: '王老师', status: '可选' },
  { id: 'K002', name: '图像识别算法', teacher: '赵老师', status: '已满' }
])

const dialogVisible = ref(false)
const editForm = ref({ id: '', name: '', teacher: '', status: '' })

function editTopic(row) {
  editForm.value = { ...row }
  dialogVisible.value = true
}
function saveTopic() {
  // TODO: 调用后端接口保存
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
function deleteTopic(row) {
  // TODO: 调用后端接口删除
  topics.value = topics.value.filter(t => t.id !== row.id)
  ElMessage.success('删除成功')
}
</script>